import React, { Component } from 'react';
import { Form, Row, Col, Icon } from 'antd';
import ShowType from '../../../routes/common/showType';
import * as commonFunc from '../../Common/commonFunc';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabMainTableComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sEmployeeName: '', /* 名称 */
      sDepartId: '', /* 部门 */
      sPositionId: '', /* 职位 */
      sEmployeeNo: '', /* 工号 */
      sMobile: '', /* 手机 */
      sPhone1: '', /* 电话 */
      sPhone2: '', /* 宅电 */
      sIdCard: '', /* 身份证 */
      sSex: '', /* 性别 */
      sDetailAddress: '', /* 地址 */
      reserveContent: '',
      expand: false,
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig, sActiveId, sId, componentName, sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (sId !== undefined && sId !== '') {
      const mainId = masterConfig.sId;
      /* 表Id */
      const value = { sId, pageSize: '', pageNum: '' };
      /* 业务数据Id */
      const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mainId}?sModelsId=${sActiveId}`;
      dispatch({
        type: 'content/getUrlPostData',
        payload: {
          url: dataUrl, sTabId, componentName, value, formRoute: '/eleemployee', returnType: 'data',
        },
      });
      /* 获取主表数据 */
    } else if (sId === '' && this.props.newIdFlag) {
      const url = `${config.server_host}getUuid/${this.props.newIdlength}`;
      dispatch({
        type: 'content/getCopyDataId',
        payload: {
          sTabId, url, formRoute: '/eleemployee', componentName,
        },
      });
    }
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sEmployeeName: mainConfig.filter(item => (item.sName.toLowerCase() === 'semployeename'))[0],
        sDepartId: mainConfig.filter(item => (item.sName.toLowerCase() === 'sdepartid'))[0],
        sPositionId: mainConfig.filter(item => (item.sName.toLowerCase() === 'spositionid'))[0],
        sEmployeeNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'semployeeno'))[0],
        sMobile: mainConfig.filter(item => (item.sName.toLowerCase() === 'smobile'))[0],
        sPhone1: mainConfig.filter(item => (item.sName.toLowerCase() === 'sphone1'))[0],
        sPhone2: mainConfig.filter(item => (item.sName.toLowerCase() === 'sphone2'))[0],
        sIdCard: mainConfig.filter(item => (item.sName.toLowerCase() === 'sidcard'))[0],
        sSex: mainConfig.filter(item => (item.sName.toLowerCase() === 'ssex'))[0],
        sDetailAddress: mainConfig.filter(item => (item.sName.toLowerCase() === 'sdetailaddress'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const {
      sId, disabled, content, dataSet,
    } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, urlData, loading, componentName,
    } = dataList;
    this.setState({ disabled, dataList: dataSet });
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        this.setState({
          dataList: urlData,
          sId: sId !== undefined ? sId : '',
        });
      }
      this.setState({ sId });
    }
    const mainConfig = this.state.masterConfig.gdsconfigformslave;
    if (Object.keys(mainConfig).length > 0) {
      const { sActiveId, app, form } = nextProps;
      const childProps = {
        app, form, sActiveId, sId, dataList, disabled,
      };
      const reserveContent = commonFunc.Reserve(mainConfig, childProps);
      this.setState({ reserveContent });
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  handleToggle = () => {
    const { expand } = this.state;
    this.setState({ expand: !expand });
  };

  render() {
    const {
      app, form, handleModalPane, dataName, dataType, handleSelectChange, onClose,
    } = this.props;
    const {
      dataList, sId, sActiveId, disabled, reserveContent, expand,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList,
      sId,
      disabled,
      handleModalPane,
      handleSelectChange,
      dataName,
      dataType,
      onClose,
    };
    const {
      sEmployeeName, sDepartId, sPositionId, sEmployeeNo, sMobile,
    } = this.state;
    const { sPhone1, sPhone2 } = this.state;
    const { sIdCard, sSex, sDetailAddress } = this.state;
    const sEmployeeNameProps = { showConfig: sEmployeeName, ...childProps };
    const sDepartIdProps = { showConfig: sDepartId, ...childProps };
    const sPositionIdProps = { showConfig: sPositionId, ...childProps };
    const sEmployeeNoProps = { showConfig: sEmployeeNo, ...childProps };
    const sMobileProps = { showConfig: sMobile, ...childProps };
    const sPhone1Props = { showConfig: sPhone1, ...childProps };
    const sPhone2Props = { showConfig: sPhone2, ...childProps };
    const sIdCardProps = { showConfig: sIdCard, ...childProps };
    const sSexProps = { showConfig: sSex, ...childProps };
    const sDetailAddressProps = { showConfig: sDetailAddress, ...childProps };
    return (
      <FormItem className={styles.searchMainForm}>
        <Row type="flex" style={{ height: expand ? 'auto' : '100px', overflow: 'hidden' }}>
          <Col
            span={6}
            order={sEmployeeName.iOrder > 24 ? 24 : sEmployeeName.iOrder}
            style={{ display: sEmployeeName.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sEmployeeName" showDefaultName="名称(*)" {...sEmployeeNameProps} />
          </Col>
          <Col
            span={6}
            order={sDepartId.iOrder > 24 ? 24 : sDepartId.iOrder}
            style={{ display: sDepartId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sDepartId" showDefaultName="部门(*)" {...sDepartIdProps} />
          </Col>
          <Col
            span={6}
            order={sPositionId.iOrder > 24 ? 24 : sPositionId.iOrder}
            style={{ display: sPositionId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sPositionId" showDefaultName="职位" {...sPositionIdProps} />
          </Col>
          <Col
            span={6}
            order={sEmployeeNo.iOrder > 24 ? 24 : sEmployeeNo.iOrder}
            style={{ display: sEmployeeNo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sEmployeeNo" showDefaultName="工号(*)" {...sEmployeeNoProps} />
          </Col>
          <Col
            span={6}
            order={sMobile.iOrder > 24 ? 24 : sMobile.iOrder}
            style={{ display: sMobile.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sMobile" showDefaultName="手机" {...sMobileProps} />
          </Col>
          <Col
            span={6}
            order={sPhone1.iOrder > 24 ? 24 : sPhone1.iOrder}
            style={{ display: sPhone1.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sPhone1" showDefaultName="电话" {...sPhone1Props} />
          </Col>
          <Col
            span={6}
            order={sPhone2.iOrder > 24 ? 24 : sPhone2.iOrder}
            style={{ display: sPhone2.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sPhone2" showDefaultName="宅电" {...sPhone2Props} />
          </Col>
          <Col
            span={6}
            order={sIdCard.iOrder > 24 ? 24 : sIdCard.iOrder}
            style={{ display: sIdCard.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sIdCard" showDefaultName="身份证" {...sIdCardProps} />
          </Col>
          <Col
            span={6}
            order={sSex.iOrder > 24 ? 24 : sSex.iOrder}
            style={{ display: sSex.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sSex" showDefaultName="性别" {...sSexProps} />
          </Col>
          <Col
            span={6}
            order={sDetailAddress.iOrder > 24 ? 24 : sDetailAddress.iOrder}
            style={{ display: sDetailAddress.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sDetailAddress" textArea showDefaultName="地址" {...sDetailAddressProps} />
          </Col>
          {reserveContent}
        </Row>
        <button className={styles.collapseA} onClick={this.handleToggle}>
          <span>{expand ? '缩略展示' : '显示全部'}</span><Icon type={expand ? 'up' : 'down'} />
        </button>
      </FormItem>
    );
  }
}

export default TabMainTableComponent;
